#include <QRectF>
#include <QDebug>
#include "mtgv.h"

MTGV::MTGV( QGraphicsScene * scene, QWidget * parent  ):
        QGraphicsView(scene,parent){
        QMap<int,QGraphicsEllipseItem> touches;
        }
void MTGV::fazBolinha(int touchId, double x, double y, int type){
    QGraphicsScene* s = this->scene();

    double w = s->width();
    double h = s->height();

    int pixel_x;
    pixel_x = x*w;
    int pixel_y;
    pixel_y = y*h;

    switch (type){
        case 0:
            if(!this->touches.contains(touchId))
              this->touches[touchId] =
                  s->addEllipse(QRectF(30,30,30,30), QPen(QColor("red")), QBrush(QColor("red"), Qt::SolidPattern));

              this->touches[touchId]->setPos(pixel_x,pixel_y);
            break;
        case 1:
           if(this->touches.contains(touchId)){
                QGraphicsEllipseItem* a = this->touches[touchId];
                a->hide();
                this->touches.remove(touchId);
                }
           break;
        case 2:
            if(this->touches.contains(touchId))
                this->touches[touchId]->setPos(pixel_x,pixel_y);
            break;

        }
}
void MTGV::onTouch(int touchId, double x, double y, int type){

    double w = this->rect().width();
    double h = this->rect().height();
    this->fazBolinha(touchId,x,y,type);
    QPoint p = QPoint(x*w,y*h);
    //QGraphicsItem *item = itemAt(p);

    QGraphicsItem *item = this->scene()->itemAt(this->mapToScene(p));
    if (item) {

        MTWidget* i = dynamic_cast<MTWidget*>(item);
        if(i != 0)
            i->onTouch(touchId,p.x(),p.y(),type);
     }

 }
